package com.whut.googleplay.http;

import com.whut.googleplay.utils.StringUtils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

/**
 * ============================================================
 * <br/>
 * 作 者 :        xyb
 * <br/>
 * 版 本 :        1.0
 * <br/>
 * 创建日期 ：     2016-05-06 下午3:31
 * <br/>
 * 描 述 ：       封装http的返回结果
 * <br/>
 * 修订历史 ：
 * <br/>
 * ============================================================
 **/
public class HttpResult {

    private HttpClient mHttpClient;
    private HttpResponse mHttpResponse;
    private HttpRequestBase mHttpRequestBase;
    private String mStr;
    private InputStream mIs;

    public HttpResult(HttpClient httpClient, HttpRequestBase httpRequestBase, HttpResponse httpResponse) {
        this.mHttpClient = httpClient;
        this.mHttpResponse = httpResponse;
        this.mHttpRequestBase = httpRequestBase;
    }

    public String getString() {
        if (!StringUtils.isEmpty(mStr)) {
            return mStr;
        }
        InputStream inputStream = getInputStream();
        ByteArrayOutputStream out = null;
        if (inputStream != null) {
            try {
                out = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024 * 4];
                int len = -1;
                while ((len = inputStream.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                byte[] data = out.toByteArray();
                mStr = new String(data, "utf-8");
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                close(inputStream, out);
            }
        }

        return mStr;
    }

    private void close(InputStream inputStream, ByteArrayOutputStream out) {
        close(out);
        close(inputStream);
        if(mHttpRequestBase!=null){
            mHttpRequestBase.abort();
        }
        if (mHttpClient != null) {
            mHttpClient.getConnectionManager().closeExpiredConnections();
        }
    }


    /**
     * 获取输入流
     *
     * @return
     */
    private InputStream getInputStream() {
        if (getStatusCode() == 200 && mIs == null) {
            HttpEntity entity = mHttpResponse.getEntity();
            try {
                mIs = entity.getContent();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mIs;
    }

    /**
     * 获取返回的状态码
     *
     * @return
     */
    private int getStatusCode() {
        StatusLine statusLine = mHttpResponse.getStatusLine();
        return statusLine.getStatusCode();
    }

    private void close(Closeable io){
        //关闭输出流
        if(io!=null){
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
